package com.rayboo.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.rayboo.model.ClubCar;
import com.rayboo.repository.ClubCarRepository;


/**
 * 
 * ClubCarService
 * 
 * @author ray
 * @date 2016年9月8日 下午8:10:41
 *
 */
@Service
public class ClubCarService {

	private static final Logger log = LoggerFactory.getLogger(ClubCarService.class);

	@Autowired
	ClubCarRepository clubCarTypeRepository;

	/**
	 * 根据条件获取列表
	 * 
	 * @param query
	 * @param pageable
	 * @return
	 */
	public List<ClubCar> find(String query, Pageable pageable) {
		List<ClubCar> clubcars = new ArrayList<>();
		try {
			clubcars = clubCarTypeRepository.find(query, pageable);
			log.info("list:" + clubcars);
		} catch (Exception e) {
			log.error("find eror", e.getMessage());
		}
		return clubcars;
	}

	/**
	 * 获取条数
	 * 
	 * @return
	 */
	public Integer count(String query) {
		int total = 0;
		try {
			total = (int) clubCarTypeRepository.count(query);
		} catch (Exception e) {
			log.error("count eror", e.getMessage());
		}
		log.info("count:" + total);
		return total;
	}

	/**
	 * 查询单个车详情信息
	 * 
	 * @param carid
	 * @return
	 */
	public ClubCar findByCarId(Integer carid) {
		ClubCar car = clubCarTypeRepository.findOne(carid);
		return car != null ? car : null;
	}

	/**
	 * 更新sname
	 * 
	 * @param carid
	 * @param sname
	 * @return
	 */
	public ClubCar updateSname(Integer carid, String sname) {
		ClubCar carType = findByCarId(carid);
		if (carType == null)
			return null;
		carType.setSname(sname);
		log.info("updateSname:" + carType);
		return clubCarTypeRepository.save(carType);
	}

	/**
	 * 更新汽车信息
	 * 
	 * @param carid
	 * @param carType
	 * @return
	 */
	public ClubCar update(Integer carid, ClubCar carType) {
		// 根据userid获取用户信息
		ClubCar car = findByCarId(carid);
		if (car == null)
			return null;
		// TODO 其他字段更新
		car.setSname(carType.getSname());
		return clubCarTypeRepository.save(car);
	}

}
